package com.lavida.project.system.attendance.controller;

import java.io.File;
import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Pattern;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lavida.common.utils.DateUtils;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.page.TableDataInfo;
import com.lavida.project.system.attendance.domain.Attendances;
import com.lavida.project.system.attendance.domain.Attendancess;
import com.lavida.project.system.attendance.service.AttendanceService;
import com.lavida.project.system.attendance.service.AttendancessService;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;


@Controller
@RequestMapping("/system/attendance")
public class AttendanceController extends BaseController{
	private String prefix = "system/attendance";
	@Autowired
	private AttendancessService attendancessService;
	@Autowired
	private IUserService userService;
	@RequiresPermissions("empatten:attendance:view")
	@RequestMapping() 
	public String accu(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return prefix+"/attendance";
	}
	@RequiresPermissions("empatten:attendance:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Attendancess attendancess,HttpServletRequest request) {	
		
		String fromtime = request.getParameter("fromtime");
		String totime = request.getParameter("totime");
		//System.out.println(fromtime + ">>>"+totime);
		startPage();
		List<Attendancess> attendancesses = attendancessService.selectAttendancessm(attendancess, fromtime, totime);
		System.out.println(attendancesses.toString());
		return getDataTable(attendancesses);
	}
	@RequiresPermissions("empatten:attendance:import")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws Exception {
		// 获取文件名，包含后缀
	    String fileName = file.getOriginalFilename();
	    // 获取文件后缀
	    String prefix = fileName.substring(fileName.lastIndexOf("."));
	    // 保存文件名
	    String saveFileName = String.valueOf(System.currentTimeMillis()) + prefix;
	    // 保存路径
	    String saveFilePath = "C:\\work\\upload\\";
	    // 保存文件
	    if (file.isEmpty()) {
			return "failed";
		}
	    File saveFile = new File(saveFilePath,saveFileName);
	    if (!saveFile.getParentFile().exists()) {
			saveFile.getParentFile().mkdirs();
			System.out.println("创建目录" + saveFile);
		}
	    System.out.println(saveFile.getPath());
	    file.transferTo(saveFile);
	    attendancessService.insertAttendancessm(saveFile.getPath());
	    return "01";
		
	} 
	@RequiresPermissions("empatten:attendance:import")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@PostMapping("/uploadd")
    @ResponseBody
    public String uploadd(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws Exception {
		// 获取文件名，包含后缀
	    String fileName = file.getOriginalFilename();
	    // 获取文件后缀
	    String prefix = fileName.substring(fileName.lastIndexOf("."));
	    // 保存文件名
	    String saveFileName = String.valueOf(System.currentTimeMillis()) + prefix;
	    // 保存路径
	    String saveFilePath = "C:\\work\\upload\\";
	    // 保存文件
	    if (file.isEmpty()) {
			return "failed";
		}
	    File saveFile = new File(saveFilePath,saveFileName);
	    if (!saveFile.getParentFile().exists()) {
			saveFile.getParentFile().mkdirs();
			System.out.println("创建目录" + saveFile);
		}
	    System.out.println(saveFile.getPath());
	    file.transferTo(saveFile);
	    attendancessService.insertAttendancessms(saveFile.getPath());
	    return "01";
		
	} 
}
